package com.blockmeta.bbs.overallserviceapplication.search.adapter;

import android.content.Context;

import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.pojo.ActiveListPOJO;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by author on 2018/1/11.
 */

public class ActiveListAdapter extends BaseAdapter<ActiveListPOJO.ListBean, BaseViewHolder> {
    private Context context;

    public ActiveListAdapter(int layoutResId, @Nullable List<ActiveListPOJO.ListBean> data, Context context) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ActiveListPOJO.ListBean item) {

        helper.setText(R.id.item_list_active_title, item.getTitle());
        helper.setText(R.id.item_list_active_views, item.getViews() + "");
        if (item.getActivity() != null) {
            if (item.getActivity().getThumb() != null && item.getActivity().getThumb().startsWith("http")) {
                ImageView imageView = helper.getView(R.id.item_list_active_img);
                Glide.with(context).load(item.getActivity().getAttachurl()).placeholder(R.drawable.yoona).into(imageView);
            }
            int timeOut = 1;//过期
            if (!TextUtils.isEmpty(item.getActivity().getStarttimefrom())) {
                int i1 = Integer.parseInt(item.getActivity().getStarttimefrom());
                long l1 = i1 * 1000L;
                long l = System.currentTimeMillis();
                TextView view = helper.getView(R.id.item_active_bg);
                if (l1 < l) {
                    timeOut = 1;
                    view.setVisibility(View.VISIBLE);
                } else {
                    timeOut = 0;
                    view.setVisibility(View.GONE);
                }
            }
            if (!TextUtils.isEmpty(item.getActivity().getStarttimefrom())) {
                //helper.setText(R.mPostName.item_list_active_time, item.getActivity().getStarttimefrom() + "");
                helper.setText(R.id.item_list_active_time, FormatUtil.getTimetoHMS(Integer.parseInt(item.getActivity().getStarttimefrom()), "yyyy-MM-dd HH:mm"));
            }
            helper.setText(R.id.item_list_active_location, item.getActivity().getPlace());
            TextView tvMoney = (TextView) helper.getView(R.id.item_list_active_money);
            if (timeOut == 1) {
                tvMoney.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            } else {
                tvMoney.setTextColor(mContext.getResources().getColor(R.color.base_color_red));
            }
            if ("0".equals(item.getActivity().getCost())) {
                tvMoney.setText("免费");
            } else {
                tvMoney.setText(item.getActivity().getCost() + "元");
            }
            LinearLayout main = helper.getView(R.id.item_list_active_main);
            main.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RouterHelper.startActivity(mContext, RouterPath.ACTIVE_DETAIL_ACTIVITY,item.getActivity().getTid() + "");
                }
            });
        }
    }
}
